package com.ilop.sthome.ui.adapter.scene;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-03-09.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class ChoseActionAdapter extends RecyclerView.Adapter<ChoseActionAdapter.ItemHolder> {

    private Context mContext;

    private List<String> mList;
    private int mPosition = -1;

    public ChoseActionAdapter(Context mContext, List<String> list) {
        this.mContext = mContext;
        this.mList = list;
    }

    public void setPosition(int mPosition) {
        this.mPosition = mPosition;
        notifyDataSetChanged();
    }

    public int getPosition() {
        return mPosition;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_chose_action, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        String name = mList.get(i);
        itemHolder.mName.setText(name);
        if (mPosition !=-1 ) {
            if (mPosition == i) {
                itemHolder.mState.setImageResource(R.mipmap.selected_48);
            }else {
                itemHolder.mState.setImageResource(R.mipmap.unselected_48);
            }
        }else {
            itemHolder.mState.setImageResource(R.mipmap.unselected_48);
        }

        View.OnClickListener choose_action = view -> LiveDataBus.get().with(CommonId.CHOOSE_ACTION).setValue(i);
        itemHolder.mState.setOnClickListener(choose_action);
        itemHolder.itemView.setOnClickListener(choose_action);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_action_name)
        TextView mName;
        @BindView(R.id.item_action_state)
        ImageView mState;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
